# -*- coding: utf8 -*-
'''
    1.4.2 图像导数
'''

from PIL import Image
from pylab import array, figure, gray, subplot, imshow, title, axis, show, zeros, np
from scipy.ndimage import filters

infile = 'img/haizei.jpg'

im = array(Image.open(infile).convert('L'))

figure()
gray()
subplot(1, 4, 1)
title('Ploting: "Haizei.jpg"')
axis('off')
imshow(im)

# Sobel导数滤波器
imx = zeros(im.shape)
filters.sobel(im, 1, imx)

subplot(1, 4, 2)
title('Ploting: X')
axis('off')
imshow(imx)

# 计算y方向导数
imy = zeros(im.shape)
filters.sobel(im, 0, imy)

subplot(1, 4, 3)
title('Ploting: Y')
axis('off')
imshow(imy)

# 梯度大小
magnitude = 255 - np.sqrt(imx**2 + imy**2)

subplot(1, 4, 4)
title('Magnitude')
axis('off')
imshow(magnitude)

show()
